package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseTypeDAO;

import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.PigHouseType;

import cn.edu.scau.cmi.hombio.service.PigHouseTypeService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for PigHouseType entities
 * 
 */

@Controller("PigHouseTypeRestController")

public class PigHouseTypeRestController {

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages PigHouseType entities
	 * 
	 */
	@Autowired
	private PigHouseTypeDAO pigHouseTypeDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for PigHouseType entities
	 * 
	 */
	@Autowired
	private PigHouseTypeService pigHouseTypeService;

	/**
	 * Create a new PigHouse entity
	 * 
	 */
	@RequestMapping(value = "/PigHouseType/{pighousetype_id}/pigHouses", method = RequestMethod.POST)
	@ResponseBody
	public PigHouse newPigHouseTypePigHouses(@PathVariable Integer pighousetype_id, @RequestBody PigHouse pighouse) {
		pigHouseTypeService.savePigHouseTypePigHouses(pighousetype_id, pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* Delete an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouseType/{pighousetype_id}/pigHouses/{pighouse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigHouseTypePigHouses(@PathVariable Integer pighousetype_id, @PathVariable Integer related_pighouses_id) {
		pigHouseTypeService.deletePigHouseTypePigHouses(pighousetype_id, related_pighouses_id);
	}

	/**
	* Create a new PigHouseType entity
	* 
	*/
	@RequestMapping(value = "/PigHouseType", method = RequestMethod.POST)
	@ResponseBody
	public PigHouseType newPigHouseType(@RequestBody PigHouseType pighousetype) {
		pigHouseTypeService.savePigHouseType(pighousetype);
		return pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype.getId());
	}

	/**
	* Select an existing PigHouseType entity
	* 
	*/
	@RequestMapping(value = "/PigHouseType/{pighousetype_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigHouseType loadPigHouseType(@PathVariable Integer pighousetype_id) {
		return pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype_id);
	}

	/**
	* Show all PigHouse entities by PigHouseType
	* 
	*/
	@RequestMapping(value = "/PigHouseType/{pighousetype_id}/pigHouses", method = RequestMethod.GET)
	@ResponseBody
	public List<PigHouse> getPigHouseTypePigHouses(@PathVariable Integer pighousetype_id) {
		return new java.util.ArrayList<PigHouse>(pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype_id).getPigHouses());
	}

	/**
	* View an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouseType/{pighousetype_id}/pigHouses/{pighouse_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigHouse loadPigHouseTypePigHouses(@PathVariable Integer pighousetype_id, @PathVariable Integer related_pighouses_id) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouses_id, -1, -1);

		return pighouse;
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Save an existing PigHouseType entity
	* 
	*/
	@RequestMapping(value = "/PigHouseType", method = RequestMethod.PUT)
	@ResponseBody
	public PigHouseType savePigHouseType(@RequestBody PigHouseType pighousetype) {
		pigHouseTypeService.savePigHouseType(pighousetype);
		return pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype.getId());
	}

	/**
	* Delete an existing PigHouseType entity
	* 
	*/
	@RequestMapping(value = "/PigHouseType/{pighousetype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigHouseType(@PathVariable Integer pighousetype_id) {
		PigHouseType pighousetype = pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype_id);
		pigHouseTypeService.deletePigHouseType(pighousetype);
	}

	/**
	* Save an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouseType/{pighousetype_id}/pigHouses", method = RequestMethod.PUT)
	@ResponseBody
	public PigHouse savePigHouseTypePigHouses(@PathVariable Integer pighousetype_id, @RequestBody PigHouse pighouses) {
		pigHouseTypeService.savePigHouseTypePigHouses(pighousetype_id, pighouses);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouses.getId());
	}

	/**
	* Show all PigHouseType entities
	* 
	*/
	@RequestMapping(value = "/PigHouseType", method = RequestMethod.GET)
	@ResponseBody
	public List<PigHouseType> listPigHouseTypes() {
		return new java.util.ArrayList<PigHouseType>(pigHouseTypeService.loadPigHouseTypes());
	}
}